$(function() {
	var zuoYeGrid = $("#ZuoYeGrid");
	var zuoyeGridAction = $("#zuoyeGridAction");
	// 初始化
	zuoYeGrid.datagrid({
		url : 'zuoye/list',
		fit : true,
		border : false,
		rownumbers : true,
		pagination : true, // 是否显示分页栏
		columns : [ [
				{
					field : 'type',
					title : '作业类型',
					align : 'center',
					width : 60
				},
				{
					field : 'zuoye_name',
					title : '作业名称',
					align : 'center',
					width : 120
				},
				{
					field : 'class_name',
					title : '布置的班级',
					align : 'center',
					width : 120
				},
				{
					field : 'phase_name',
					title : '阶段',
					align : 'center',
					width : 60
				},
				{
					field : 'chapter_name',
					title : '章节',
					align : 'center',
					width : 170
				},
				{
					field : 'option',
					title : '选择题数量',
					align : 'center',
					width : 80
				},
				{
					field : 'panduan',
					title : '判断题数量',
					align : 'center',
					width : 80
				},
				{
					field : 'daima',
					title : '代码题数量',
					align : 'center',
					width : 80
				},
				{
					field : 'user_name',
					title : '布置老师',
					align : 'center',
					width : 80
				},
				{
					field : 'state',
					title : '作业状态',
					align : 'center',
					width : 80
				},
				{
					field : 'buzhi_time',
					title : '布置时间',
					align : 'center',
					width : 120,
					formatter : function(value, row, index) {
						if (value == null) {
							return "null";
						}
						var c = new Date(value);// 传入一个时间戳
						c.toLocaleString();// 返回本地时间
						var d = new Date(c);
						var formatIt = d.getFullYear() + '-'
								+ (d.getMonth() + 1) + '-' + d.getDate() + ' '
								+ d.getHours() + ':' + d.getMinutes() + ':'
								+ d.getSeconds();
						return formatIt;

					}
				},
				{
					field : 'end_time',
					title : '完成时间',
					align : 'center',
					width : 120,
					formatter : function(value, row, index) {
						if (value == null) {
							return "null";
						}
						var c = new Date(value);// 传入一个时间戳
						c.toLocaleString();// 返回本地时间
						var d = new Date(c);
						var formatIt = d.getFullYear() + '-'
								+ (d.getMonth() + 1) + '-' + d.getDate() + ' '
								+ d.getHours() + ':' + d.getMinutes() + ':'
								+ d.getSeconds();
						return formatIt;

					}
				},
				{
					field : 'add_plus',
					title : '备注',
					align : 'center',
					width : 80,
					formatter : function(value) {
						if (value==null||value=='') {
							return "<span title='暂无'>暂无</span>";
						}else{
							return "<span title='" + value + "'>" + value
							+ "</span>";
						}
					}
				},
				{
					field : 'ac',
					title : '操作',
					align : 'center',
					width : 100,
					formatter : function(value, row, index) {
						/*var bts = [];
						bts.push('<a data-index="' + index
								+ '" class="ctr edit">编辑</a>');*/
						/*bts.push('<a data-index="' + index
								+ '" class="ctr queryInfo">查询详情</a>');*/
						return zuoyeGridAction.children("a.ctr").attr('data-index', index)
								.end().html();
					}
				} ] ],
		toolbar :"#zuoyeGridToolbar"
	});
	var phase = $("#phase");
	var className = $("#className");
	$("#zuoyeSearch").on('click', function() {
		zuoYeGrid.datagrid("load", {
			optionCount : phase.val(),
			panDuanCount : className.val()
		});
	});
	// 给重置按钮添加事件
	$("#zuoyeReset").on('click', function() {
		zuoYeGrid.datagrid("load", {})
	});

	var gridPanel = zuoYeGrid.datagrid("getPanel");
	gridPanel.on("click", "a.edit", function() {
		var index = this.dataset.index;
		var rows = zuoYeGrid.datagrid("getRows");
		var id = rows[index].zuoye_id;
		var name = rows[index].class_name;
		teacherBuzhi(id, name);
	}).on("click", "a.create", function() {
		teacherBuzhi();yuejuan
	}).on("click", "a.yuejuan", function() {
		
			var bol = $('#tabs').tabs('exists', '阅卷');
			if (bol) {
				$('#tabs').tabs('select', '阅卷')
			} else {
				$('#tabs').tabs('add', {
					title : '阅卷',
					closable : true,
					href : 'jiaoxue/jx-teacher-yuejuan'
				});
			
		}
	})

	function teacherBuzhi(id, name) {
		var dialog = $("<div/>").dialog(
				{
					href : 'jiaoxue/jx-teacher-buzhi' + (id ? ("/" + id) : "")
							+ (name ? ("/" + name) : ""),
					title : '布置作业',
					height : '620',
					width : '620',
					modal : true,
					onClose : function() {
						$(this).dialog("destroy");
					},
					buttons : [
							{
								iconCls : 'icon-save',
								text : '随机生成作业',
								handler : function() {
									// 获取表单的内容
									var buZhiForm = $("#buZhiForm");
									if (buZhiForm.form('validate')) {
										$.post("zuoye/save", buZhiForm
												.serialize(), function(rs) {
											if (rs.success) {
												zuoYeGrid.datagrid("reload");
												dialog.dialog("close");
											} else {
												$.messager.alert("提示",
														rs.message)
											}
										})
									}
								}
							}, {
								iconCls : 'icon-clear',
								text : '取消',
								handler : function() {
									dialog.dialog("close");
								}
							} ]
				});
	}
});